package com.example.zhaojianxu.signin.englishgame.start

import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.View
import com.example.zhaojianxu.signin.R
import com.example.zhaojianxu.signin.englishgame.constance.Constance
import com.example.zhaojianxu.signin.englishgame.http.EGStartEntity
import de.greenrobot.event.EventBus
import kotlinx.android.synthetic.main.item_start_view.view.*

/**
 * create by @zhaojianxu on 2019/2/26
 */
class StartViewHolder(val rootView: View) : RecyclerView.ViewHolder(rootView) {

    //是否被选中 one
    var flagOne = false
    //是否被选中 two
    var flagTwo = false
    var mModel: EGStartEntity? = null

    init {
        EventBus.getDefault().register(this)

        rootView.item_start_1.setOnClickListener {
            flagOne = !flagOne
            if (flagOne) {
                Constance.KEY = mModel?.china.toString()
                Constance.POSITION_ONE = adapterPosition
                EventBus.getDefault().post(StartOneEvent(adapterPosition))
            } else {
                Constance.KEY = ""
                Constance.POSITION_ONE = -1
                it.setBackgroundResource(R.drawable.english_game_start_card_before)
            }
        }

        rootView.item_start_2.setOnClickListener {
            flagTwo = !flagTwo
            if (flagTwo) {
                Constance.VALUE = mModel?.mokeE.toString()
                Constance.POSITION_TWO = adapterPosition
                EventBus.getDefault().post(StartTwoEvent(adapterPosition))
            } else {
                Constance.VALUE = ""
                Constance.POSITION_TWO = -1
                it.setBackgroundResource(R.drawable.english_game_start_card_before)
            }
        }
    }

    fun bindData(model: EGStartEntity) {
        mModel = model

        rootView.visibility
        rootView.item_start_1.let {
            it.visibility = View.VISIBLE
            it.text = model.china
            it.setBackgroundResource(R.drawable.english_game_start_card_before)
        }

        rootView.item_start_2.let {
            it.visibility = View.VISIBLE
            it.text = model.mokeE
            it.setBackgroundResource(R.drawable.english_game_start_card_before)
        }
    }

    fun onEvent(event: StartOneEvent) {
        if (event.id != adapterPosition) {
            flagOne = false
            rootView.item_start_1.setBackgroundResource(R.drawable.english_game_start_card_before)
        } else {
            if (Constance.KEY.isNotEmpty()) {
//                Constance.POSITION_ONE = adapterPosition
                rootView.item_start_1.setBackgroundResource(R.drawable.english_game_start_card_after)
            }
            if (Constance.VALUE.isNotEmpty()) {
                if (mModel?.english.equals(Constance.VALUE)) {
                    rootView.item_start_1.setBackgroundResource(R.drawable.english_game_start_card_after)
                    EventBus.getDefault().post(StartVisibilityEvent())

                } else {
                    rootView.item_start_1.setBackgroundResource(R.drawable.english_game_start_card_before)
                }
            }
        }
    }

    fun onEvent(event: StartTwoEvent) {
        if (event.id != adapterPosition) {
            flagTwo = false
            rootView.item_start_2.setBackgroundResource(R.drawable.english_game_start_card_before)
        } else {
            if (Constance.VALUE.isNotEmpty()) {
                rootView.item_start_2.setBackgroundResource(R.drawable.english_game_start_card_after)
            }
            if (Constance.KEY.isNotEmpty()) {
                if (mModel?.mokeC.equals(Constance.KEY)) {
                    rootView.item_start_2.setBackgroundResource(R.drawable.english_game_start_card_after)
                    EventBus.getDefault().post(StartVisibilityEvent())

                } else {
                    rootView.item_start_2.setBackgroundResource(R.drawable.english_game_start_card_before)
                }
            }
        }
    }

    fun onEvent(event: StartVisibilityEvent) {

        Constance.KEY = ""
        Constance.VALUE = ""
        if (adapterPosition == Constance.POSITION_ONE) {
            flagOne = false
            rootView.item_start_1.visibility = View.GONE
            Log.d(
                "TAGGGGG",
                "one    this = ${this@StartViewHolder}     position = ${Constance.POSITION_ONE} Constance.KEY = ${Constance.KEY}  ${mModel.toString()}"
            )
        }

        if (adapterPosition == Constance.POSITION_TWO) {
            flagTwo = false
            rootView.item_start_2.visibility = View.GONE
            Log.d(
                "TAGGGGG",
                "two    this = ${this@StartViewHolder}       position = ${Constance.POSITION_TWO}  Constance.VALUE = ${Constance.VALUE}    ${mModel.toString()}"
            )
        }
    }

    fun onEvent(event: UnregisterEvent){
        EventBus.getDefault().unregister(this)
    }
}